package edu.ucla.wanda.db;

import java.util.Comparator;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;


public class WandaSensorMeasurement {
	private String id, subjectId, sensorId, startTime, endTime, value;
	private WandaSensorSpec sensorSpec;
	
	private Long startTimeLong, endTimeLong;
	private Double valueDouble;
	
	public WandaSensorMeasurement(JsonElement jsonElement) {
		JsonObject object = jsonElement.getAsJsonObject();
		
		subjectId = object.getAsJsonPrimitive("SubjectId").getAsString();
		value = object.getAsJsonPrimitive("Value").getAsString();
		valueDouble = Double.valueOf(value);
		sensorId = object.getAsJsonPrimitive("SensorId").getAsString();
		id = object.getAsJsonPrimitive("Id").getAsString();
		startTime = object.getAsJsonPrimitive("StartTime").getAsString();
		startTimeLong = Long.valueOf(startTime);
		if(object.has("EndTime"))
		{
			endTime = object.getAsJsonPrimitive("EndTime").getAsString();
			endTimeLong = Long.parseLong(endTime);
		}
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}


	/**
	 * @return the subjectId
	 */
	public String getSubjectId() {
		return subjectId;
	}


	/**
	 * @return the sensorId
	 */
	public String getSensorId() {
		return sensorId;
	}


	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}


	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @return the sensorSpec
	 */
	public WandaSensorSpec getSensorSpec() {
		return sensorSpec;
	}

	
	/**
	 * @param sensorSpec the sensorSpec to set
	 */
	public void setSensorSpec(WandaSensorSpec sensorSpec) {
		this.sensorSpec = sensorSpec;
	}

	public String getDayString()
	{
		Long measTime = Long.parseLong(startTime);
		
		if(DateMidnight.now().isBefore(measTime))
		{
			return "Today";
		}
		
		if(DateMidnight.now().minusDays(1).isBefore(measTime))
		{
			return "Yesterday";
		}
		
		DateTime meas = new DateTime(measTime);
		
		if(DateTime.now().year() == meas.year())
		{
			return meas.toString("MMM dd");
		}
		
		else
		{
			return meas.toString("MMM dd, YYYY");
		}
	}
	
	public String getTimeString()
	{
		DateTime meas = new DateTime(Long.parseLong(startTime));
		
		return meas.toString("h:mm:ss a");
	}
	
	public static Comparator<WandaSensorMeasurement> getSensorTimeComparatorAsc()
	{
		return new Comparator<WandaSensorMeasurement>()
		{
			public int compare(WandaSensorMeasurement meas1, WandaSensorMeasurement meas2)
			{
				long long1 = Long.parseLong(meas1.getStartTime());
		    	long long2 = Long.parseLong(meas2.getStartTime());
		   
		    	return((int)(long1 - long2));
			}
	    };
	}
	
	public int compareReadingTimeLong(Object meas1, Object meas2)
	{
    	return (int) ((Long) meas1).compareTo((Long) meas2);
	}
	
	public int compareReadingValueDouble(Object meas1, Object meas2)
	{   
    	return (int) ((Double) meas1).compareTo((Double) meas2);
	}
	
	public static Comparator<WandaSensorMeasurement> getSensorTimeComparatorDesc()
	{
		return new Comparator<WandaSensorMeasurement>()
		{
			public int compare(WandaSensorMeasurement meas1, WandaSensorMeasurement meas2)
			{
				long long1 = Long.parseLong(meas1.getStartTime());
		    	long long2 = Long.parseLong(meas2.getStartTime());
		   
		    	return((int)(long2 - long1));
			}
	    };
	}
	
	
	public static Comparator<WandaSensorMeasurement> getSensorValueComparatorAsc()
	{
		return new Comparator<WandaSensorMeasurement>()
		{
			public int compare(WandaSensorMeasurement meas1, WandaSensorMeasurement meas2)
			{
				double d1 = Double.parseDouble(meas1.getValue());
		    	double d2 = Double.parseDouble(meas2.getValue());
		   
		    	return((int)(d1 - d2));
			}
	    };
	}
	
	public static Comparator<WandaSensorMeasurement> getSensorValueComparatorDesc()
	{
		return new Comparator<WandaSensorMeasurement>()
		{
			public int compare(WandaSensorMeasurement meas1, WandaSensorMeasurement meas2)
			{
				double d1 = Double.parseDouble(meas1.getValue());
		    	double d2 = Double.parseDouble(meas2.getValue());
		   
		    	return((int)(d2 - d1));
			}
	    };
	}

	/**
	 * @return the startTimeLong
	 */
	public long getStartTimeLong() {
		return startTimeLong;
	}

	/**
	 * @return the endTimeLong
	 */
	public long getEndTimeLong() {
		return endTimeLong;
	}

	/**
	 * @return the valueDouble
	 */
	public double getValueDouble() {
		return valueDouble;
	}
}
